package com.zzj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzj.bean.TabLogin;
import com.zzj.bean.TableEmployee;
import com.zzj.dao.TabLoginMapper;
import com.zzj.dao.TableEmployeeMapper;
import com.zzj.service.TableEmployeeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * InnoDB free: 8192 kB 服务实现类
 * </p>
 *
 * @author 花
 * @since 2022-03-04
 */
@Service
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
public class TableEmployeeServiceImpl extends ServiceImpl<TableEmployeeMapper, TableEmployee> implements TableEmployeeService {
    @Resource
    private TableEmployeeMapper employeeMapper;
    @Resource
    private TabLoginMapper loginMapper;

    @Override
    public PageInfo<TableEmployee> getEmployees(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TableEmployee> employees = employeeMapper.getEmployees();
        return new PageInfo<>(employees);
    }

    @Override
    public boolean addEmp(TableEmployee employee) {
        employee.setUcjtime(new Date());
        employee.setUstatus("1");
        employee.getTabLogin().setLstatus("1");
        int b1 = employeeMapper.insert(employee);
        employee.getTabLogin().setUid(employee.getUid());
        int b2 = loginMapper.insert(employee.getTabLogin());
        return b1==1 && b2==1;
    }

    @Override
    public boolean delEmp(String uid) {
        TableEmployee employee = employeeMapper.selectById(uid);
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid",employee.getUid());
        List<TabLogin> logins = loginMapper.selectByMap(map);
        if (employee.getUstatus().equals("1")){
            employee.setUstatus("0");
            logins.get(0).setLstatus("0");
        }else {
            employee.setUstatus("1");
            logins.get(0).setLstatus("1");
        }
        int i1 = employeeMapper.updateById(employee);
        int i2 = loginMapper.updateById(logins.get(0));
        return i1==1 && i2==1;
    }

    @Override
    public boolean updateEmp(TableEmployee employee) {
        employee.setUxgtime(new Date());
        System.out.println("date = " + new Date());
        employee.getTabLogin().setUid(employee.getUid());
        int i1 = employeeMapper.updateById(employee);
        int i2 = loginMapper.updateById(employee.getTabLogin());
        return i1==1 && i2==1;
    }

    @Override
    public PageInfo<TableEmployee> getEmpByLike(int pageNum, int pageSize, String column, String val) {
        PageHelper.startPage(pageNum,pageSize);
        List<TableEmployee> list = employeeMapper.getEmpByLike(column, val);
        return new PageInfo<>(list);
    }
}
